// merchant//flyer/flyerCode/index.js
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var windowWidth = wx.getSystemInfoSync().windowWidth * 0.96;
    var canvasHeight = windowWidth * 1.52;
    this.setData({
      userId: options.userId,
      windowWidth: windowWidth,
      canvasHeight: canvasHeight
    })
    this.getWxaCodeFile(options.flyerCode);
  },

  //我的免单码
  getWxaCodeFile: function (flyerCode) {
    let that = this;
    wx.showLoading({
      title: '生成中...',
      mask: true,
    })
    let qrCode = utils.formatAvater(flyerCode, 'https://static.711688.net.cn/upload/share/');
    wx.downloadFile({
      url: qrCode,
      success: function (res) {
        if (res.statusCode === 200) {
          var codeSrc = res.tempFilePath;
          that.getBgimg(codeSrc);
        } else {
          wx.showToast({
            title: '小程序码下载失败！',
            icon: 'none',
            duration: 2000,
            success: function () {
              var codeSrc = "";
              that.getBgimg(codeSrc);
            }
          })
        }
      }
    })
  },

  getBgimg: function (codeSrc) {
    let that = this;
    let bgUrl = 'https://static.711688.net.cn/upload/card/source/ua_2105071001373915746.jpg';
    wx.downloadFile({
      url: bgUrl,
      success: function (res) {
        if (res.statusCode === 200) {
          var bgImg = res.tempFilePath;
          that.sharePosteCanvasFlyer(codeSrc, bgImg);
        } else {
          wx.showToast({
            title: '图片下载失败！',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  sharePosteCanvasFlyer: function (codeSrc, bgImg) {
    let that = this;
    that.setData({
      showpost: true
    })
    const ctx = wx.createCanvasContext('myCanvas', that);
    const query = wx.createSelectorQuery().in(this);
    query.select('#canvas-container').boundingClientRect(function (rect) {
      var height = rect.height;
      var width = rect.width;
      ctx.drawImage(bgImg, 0, 0, width, height);
      let codeWidth = width * 280 / 720;
      if (codeSrc) {
        ctx.drawImage(codeSrc, (width - codeWidth) / 2, width * 217 / 720, width * 280 / 720, width * 280 / 720);
      }
    }).exec()
    setTimeout(function () {
      ctx.draw();
      wx.hideLoading();
    }, 1000)
  },
  //点击保存到相册
  saveShareImg: function () {
    var that = this;
    wx.showLoading({
      title: '正在保存',
      mask: true,
    })
    setTimeout(function () {
      wx.canvasToTempFilePath({
        canvasId: 'myCanvas',
        success: function (res) {
          wx.hideLoading();
          var tempFilePath = res.tempFilePath;
          wx.saveImageToPhotosAlbum({
            filePath: tempFilePath,
            success(res) {
              wx.showModal({
                content: '图片已保存到相册，赶紧晒一下吧~',
                showCancel: false,
                confirmText: '好的',
                confirmColor: '#333',
                success: function (res) {
                  if (res.confirm) { }
                },
                fail: function (res) {
                }
              })
            },
            fail: function (res) {
              wx.showToast({
                title: '请删掉小程序再重新授权允许保存至相册',
                icon: 'none',
                duration: 2000
              })
            }
          })
        },
        fail: function (err) {
        }
      }, that);
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})